<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\common\controller\Admin;
use think\facade\Db;
use think\Request;

class UserIndex extends Admin
{
    protected $table_id = false;
    protected $with = [];
    public function initialize()
    {
        $this->menu_input = [
            'all' => [
            ],
            'add' => [
            ],
            'upload' => [
            ],
        ];
        if(input('parent')){
            $this->menu_input['all'][0]['select']['where'] = ['key'=>'parent_id','value'=>input('parent')];
        }
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    public function table_mage(&$data = [])
    {

        $data = [
            ['field' => 'create_time', 'title' => '注册时间'],
            ['field' => 'nickname', 'title' => '用户昵称'],
            ['field' => 'integral', 'title' => '积分'],
            ['title' => '操作', 'templet' => '#list_setting']
        ];
        return parent::table_mage($data); // TODO: Change the autogenerated stub
    }
    public function index()
    {
        $ProvinceCityArea_model = app('app\admin\model\ProvinceCityArea');
        $sf = $ProvinceCityArea_model->where('type',1)->select()->toArray();
        $this->assign('sf',$sf);
        return parent::index(); // TODO: Change the autogenerated stub
    }

    public function index_handle(&$data)
    {
        $data['data'] = array_map(function($v){
            return $v;
        },$data['data']);
        return parent::index_handle($data); // TODO: Change the autogenerated stub
    }
    public function index_where(&$where)
    {
        if(!empty($where)){
            foreach ($where as $k=>$v){
                if($v[0] == 'nickname'){
                    $business = app('app\admin\model\Business')->where('nickname','like','%'.$v.'%')->select();
                    if(!empty($business) && !empty($business->toArray())){
                        $business = $business->toArray();
                        $ids = [];
                        foreach ($business as $vv){
                            $ids[] = $vv['id'];
                        }
                        $ids = implode(',',$ids);
                        $bus_user = Db::name('business_user')->where('business_id','in',$ids)->select();
                        if(!empty($bus_user)){
                            $ids = [];
                            foreach ($bus_user as $vv)
                            {
                                $ids[] = $vv['user_index_id'];
                            }
                            $ids = implode(',',$ids);
                            $where[] = ['id','in',$ids];
                        }else{
                            $where[] = ['id','=',-1];
                        }
                    }else{
                        $where[] = ['id','=',-1];
                    }
                }elseif($v[0] == 'time_time'){
                    $time = explode('到',$v[2]);
                    $where[] = ['create_time','>=',trim($time[0])];
                    $where[] = ['create_time','<=',trim($time[1])];
                    unset($where[$k]);
                }
            }
        }
        return parent::index_where($where); // TODO: Change the autogenerated stub
    }
    //编辑-页面
    public function upload()
    {
        $id = input('id');
        $data = $this->model->where('id', $id)->with($this->with)->find()->toArray();

        $this->assign('data', $data);
        return view();
    }
    
    //列表-数据接口
    public function datalist()
    {
        if (is_array($this->with)) {
            $with = $this->with;
        } else {
            $with = [];
        }
        $data = param();
        $sheng = $data['sheng'] ?? '';
        $city = $data['city'] ?? '';
        unset($data['sheng']);
        unset($data['city']);
        unset($data['page']);
        unset($data['limit']);
        unset($data['parent']);
        if(empty($city) && !empty($sheng)){
            //只有省，没选市
            $bus_data = app('app\admin\model\Business')->where('province_id','=',$sheng)->select();
            if(!empty($bus_data) && !empty($bus_data->toArray())){
                $bus_data = $bus_data->toArray();
                $ids = [];
                foreach ($bus_data as $v)
                {
                    $ids[] = $v['id'];
                }
                $ids = implode(',',$ids);
                $bus_user = Db::name('business_user')->where('business_id','in',$ids)->select();
                if(!empty($bus_user)){
                    $ids = [];
                    foreach ($bus_user as $vv)
                    {
                        $ids[] = $vv['user_index_id'];
                    }
                    $ids = implode(',',$ids);
                    $this->where[] = ['id','in',$ids];
                }else{
                    $this->where[] = ['id','=',-1];
                }
            }else{
                $this->where[] = ['id','=',-1];
            }
        }elseif(!empty($city)){
            //有市，就不需要管省了
            $bus_data = app('app\admin\model\Business')->where('city_id','=',$city)->select();
            if(!empty($bus_data) && !empty($bus_data->toArray())){
                $bus_data = $bus_data->toArray();
                $ids = [];
                foreach ($bus_data as $v)
                {
                    $ids[] = $v['id'];
                }
                $ids = implode(',',$ids);
                $bus_user = Db::name('business_user')->where('business_id','in',$ids)->select();
                if(!empty($bus_user)){
                    $ids = [];
                    foreach ($bus_user as $vv)
                    {
                        $ids[] = $vv['user_index_id'];
                    }
                    $ids = implode(',',$ids);
                    $this->where[] = ['id','in',$ids];
                }else{
                    $this->where[] = ['id','=',-1];
                }
            }else{
                $this->where[] = ['id','=',-1];
            }
        }
        foreach ($data as $k => $v) {
            if (in_array($k, ['name', 'title'])) {
                $this->where[] = [$k, 'like', '%' . $v . '%'];
            } elseif ($k == 'create_time') {
                $arr = b_arr($v, '-');
                $first = b_str([$arr[0], $arr[1], $arr[2]], '-');
                $last = b_str([$arr[3], $arr[4], $arr[5]], '-');
                $this->where[] = ['create_time', '>=', trim($first)];
                $this->where[] = ['create_time', '<=', trim($last)];
            } else {
                $this->where[] = [$k, '=', $v];
            }
        }

        $order = $this->order ?: 'id desc';
        $this->index_where($this->where);
        if(!empty($this->where)){
            $this->where = array_values($this->where);
        }
        $list = $this->model->where($this->where)->order($order)->with($with)->paginate($this->limit)->toArray();
        $this->index_handle($list);
        return layui($list['data'], $list['total']);
    }
}
